﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DlrBasic.Runtime
{
    /// <summary>
    /// Contains some BASIC code debug/diagnostics information such as
    /// line numbers for statement number, statements symbolic names, etc.
    /// </summary>
    public sealed class DlrBasicDebugInfo
    {
        int[] lineNumbers;
        int[] states;
        string[] statementNames;

        public DlrBasicDebugInfo(int[] lineNumbers, int[] states, string[] statementNames)
        {
            this.lineNumbers = lineNumbers;
            this.states = states;
            this.statementNames = statementNames;
        }

        public int GetLineNumber(int state)
        {
            int index = Array.BinarySearch(states, state);
            if (index < 0) index = ~index - 1;
            return lineNumbers[index];
        }

        public void GetLineNumberAndOffset(int state, out int lineNumber, out int stateOffset)
        {
            int index = Array.BinarySearch(states, state);
            if (index < 0) index = ~index - 1;
            lineNumber = lineNumbers[index];
            stateOffset = state - states[index];
        }

        public string GetStatementName(int state)
        {
            return statementNames[state];
        }
    }
}
